﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Group_Mail
{
    public partial class frmGroupCreator : Form
    {
        public frmGroupCreator()
        {
            InitializeComponent();
        }

        public Group Group { get; set; }

        private Status CurrentStatus { get; set; }

        private void frmGroupCreator_Paint(object sender, PaintEventArgs e)
        {
            var pen = new Pen(ColorTranslator.FromHtml("#ACA899"), 1) {DashStyle = DashStyle.Dash};
            e.Graphics.DrawLine(pen, new Point(0, 55), new Point(Width, 55));
            e.Graphics.DrawLine(pen, new Point(0, Height - 60), new Point(Width, Height - 60));
        }

        private void SwitchStatus()
        {
            switch (CurrentStatus)
            {
                case Status.EnterName:
                    lblStatus.Text = "Please enter name for this new group";
                    btnPrevious.Enabled = false;
                    btnNext.Text = "Next";
                    wizardControl.SelectedTab = tabEnterName;
                    break;
                case Status.EnterFields:
                    lblStatus.Text = "Specify the fields to be stored for each recipient";
                    btnPrevious.Enabled = true;
                    btnNext.Text = "Next";
                    wizardControl.SelectedTab = tabEnterFields;
                    break;
                case Status.ChoiseEmailField:
                    lblStatus.Text = "Select the field that containts the recipient email address";
                    btnPrevious.Enabled = true;
                    btnNext.Text = "Create";
                    wizardControl.SelectedTab = tabChoiseEmailField;
                    ddlFields.Items.Clear();
                    foreach (DataGridViewRow row in dataGridView.Rows)
                    {
                        var value = row.Cells[0].Value;
                        if (value != null)
                        {
                            ddlFields.Items.Add(value);
                        }
                    }
                    ddlFields.SelectedIndex = 0;
                    break;
                case Status.Finish:
                    Group = new Group {Id = Guid.NewGuid().ToString(), Name = txtGroupName.Text.Trim()};
                    foreach (DataGridViewRow row in dataGridView.Rows)
                    {
                        var field = new Field
                                        {
                                            Name = row.Cells[0].Value.ToString().Trim()
                                        };
                        try
                        {
                            field.Size = Convert.ToInt32(row.Cells[1].Value.ToString().Trim());
                        }
                        catch
                        {
                            throw new ArgumentException("Field size was not in a number correct format.");
                        }

                        if (string.IsNullOrEmpty(field.Name))
                        {
                            throw new ArgumentException("Field name cannot null or empty.");
                        }

                        if (field.Size <= 0)
                        {
                            throw new ArgumentException("Field size cannot less than zero.");
                        }

                        Group.Fields.Add(field);
                    }

                    Group.EmailField = ddlFields.SelectedItem.ToString();

                    if (string.IsNullOrEmpty(Group.Name))
                    {
                        throw new ArgumentException("Group name cannot null or empty.");
                    }

                    if (string.IsNullOrEmpty(Group.EmailField))
                    {
                        throw new ArgumentException("Group email field cannot null or empty.");
                    }

                    DialogResult = DialogResult.OK;
                    Close();
                    break;
            }
        }

        private void frmGroupCreator_Load(object sender, EventArgs e)
        {
            SwitchStatus();
            dataGridView.Rows.Add(new object[] {"FullName", "255"});
            dataGridView.Rows.Add(new object[] {"Email", "255"});
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            switch (CurrentStatus)
            {
                case Status.EnterName:
                    CurrentStatus = Status.EnterFields;
                    break;
                case Status.EnterFields:
                    if (dataGridView.Rows.Count == 0)
                    {
                        MessageBox.Show("Please insert fileds, at least a field must be defined to create group",
                                        "Group Mail", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    CurrentStatus = Status.ChoiseEmailField;
                    break;
                case Status.ChoiseEmailField:
                    CurrentStatus = Status.Finish;
                    break;
            }
            SwitchStatus();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            switch (CurrentStatus)
            {
                case Status.EnterFields:
                    CurrentStatus = Status.EnterName;
                    break;
                case Status.ChoiseEmailField:
                    CurrentStatus = Status.EnterFields;
                    break;
            }
            SwitchStatus();
        }

        private void btnDefaultFields_Click(object sender, EventArgs e)
        {
            dataGridView.Rows.Clear();
            dataGridView.Rows.Add(new object[] {"FullName", "255"});
            dataGridView.Rows.Add(new object[] {"Email", "255"});
        }

        private void btnNewField_Click(object sender, EventArgs e)
        {
            var index = dataGridView.Rows.Add(new object[] {"New Field", "255"});
            dataGridView.CurrentCell = dataGridView[0, index];
            dataGridView.BeginEdit(true);
        }

        private void btnRemoveField_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                dataGridView.Rows.Remove(dataGridView.SelectedRows[0]);
            }
        }

        private void btnModifyField_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count > 0)
            {
                dataGridView.CurrentCell = dataGridView.SelectedRows[0].Cells[0];
                dataGridView.BeginEdit(true);
            }
        }

        #region Nested type: Status

        private enum Status
        {
            EnterName,
            EnterFields,
            ChoiseEmailField,
            Finish,
        }

        #endregion
    }
}